﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net.Appender;
using log4net.Config;

namespace ClassLibrary1
{
    public partial class LogConsole : UserControl, IAppender
    {
        public LogConsole()
        {
            InitializeComponent();

            XmlConfigurator.Configure();

            ((log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetLoggerRepository()).Root.AddAppender(this);

        }

        public void SetText(string message, Color color)
        {
            richTextBox1.SelectionColor = color;
            richTextBox1.SelectedText = Environment.NewLine + message;

            //this.richTextBox1.AppendText(message + Environment.NewLine);
        }

        #region IAppender Members

        public void Close()
        {
            
        }

        public void DoAppend(log4net.Core.LoggingEvent loggingEvent)
        {
            if (loggingEvent.Level == log4net.Core.Level.Warn)
            {
                SetText(String.Format("log4net - {0}: {1}", loggingEvent.Level.Name, loggingEvent.MessageObject.ToString()), Color.Yellow);
            }

            if (loggingEvent.Level == log4net.Core.Level.Debug)
            {
                SetText(String.Format("log4net - {0}: {1}", loggingEvent.Level.Name, loggingEvent.MessageObject.ToString()), Color.Green);
            }

            if (loggingEvent.Level == log4net.Core.Level.Error)
            {
                SetText(String.Format("log4net - {0}: {1}", loggingEvent.Level.Name, loggingEvent.MessageObject.ToString()), Color.Red);
            }

            if (loggingEvent.Level == log4net.Core.Level.Info)
            {
                SetText(String.Format("log4net - {0}: {1}", loggingEvent.Level.Name, loggingEvent.MessageObject.ToString()), Color.White);
            }
        }

        #endregion
    }
}
